﻿using Abp.Application.Services;
using Abp.Application.Services.Dto;
using Abp.Domain.Repositories;
using Abp.ObjectMapping;
using Abp.Runtime.Session;
using OplugAbpProject.CMS.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace OplugAbpProject.CMS
{
    public class ArticleAppService : AsyncCrudAppService<Article, ArticleDto, long, PagedResultRequestDto, CreateArticleDto, UpdateArticleDto>, IArticleAppService
    {
        private readonly IObjectMapper _objectMapper;

        public ArticleAppService(IRepository<Article, long> repository, IObjectMapper objectMapper) : base(repository)
        {
            _objectMapper = objectMapper;
            AbpSession = NullAbpSession.Instance;
        }
        public async Task<List<ArticleDto>> GetListsAsync(int SkipCount, int MaxResultCount, ArticleParam args)
        {
            List<ArticleDto> datas = new List<ArticleDto>();
            Expression<Func<Article, bool>> expression = o => o.Id > 0;
            if (args != null)
            {
                if (args.CategoryId > 0)
                    expression = expression.ExpressionAnd(o => o.CategoryId == args.CategoryId);

                if (!string.IsNullOrEmpty(args.keywords))
                {
                    expression = expression.ExpressionAnd(o => o.Title != null && o.Title.Contains(args.keywords));
                    expression = expression.ExpressionAnd(o => o.SubTitle != null && o.SubTitle.Contains(args.keywords));
                    expression = expression.ExpressionAnd(o => o.Contents != null && o.Contents.Contains(args.keywords));
                }
                if (!string.IsNullOrEmpty(args.Tags))
                    expression = expression.ExpressionAnd(o => o.Tags != null && o.Tags.Contains(args.Tags));
                if (!string.IsNullOrEmpty(args.Author))
                    expression = expression.ExpressionAnd(o => o.Author == args.Author);
                if (args.TimeSpan.Length > 1)
                {
                    DateTime sdate = DateTime.Parse(args.TimeSpan[0]);
                    DateTime edate = DateTime.Parse(args.TimeSpan[1]);
                    expression = expression.ExpressionAnd(o => o.CreationTime >= sdate && o.CreationTime <= edate);
                }
                if (args.ModTimeSpan.Length > 1)
                {
                    DateTime sdate = DateTime.Parse(args.ModTimeSpan[0]);
                    DateTime edate = DateTime.Parse(args.ModTimeSpan[1]);
                    expression = expression.ExpressionAnd(o => o.LastModificationTime >= sdate && o.LastModificationTime <= edate);
                }
            }
            var result = await Repository.GetAllListAsync(expression);
            result = result.Skip(SkipCount).Take(MaxResultCount).ToList();
            return _objectMapper.Map(result, datas);
        }

        public async Task<long> CreateOrUpdateAsync(CreateArticleDto input)
        {            
            var obj = ObjectMapper.Map<Article>(input);
            return await Repository.InsertOrUpdateAndGetIdAsync(obj);
        }

        public async Task DeleteAsync(long[] ids)
        {
            await Repository.DeleteAsync(o => ids.Contains(o.Id));
        }

    }
}
